package cn.neusoft.interceptor;

import cn.neusoft.vo.ResultBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 在进行拦截的时候 ，用户也能看到商家和餐品的一些信息
 *
 */
public class UserLoginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		//先进行验证
		//验证成功再进行放行
		HttpSession session=request.getSession();
		Object loginUser=session.getAttribute("loginUser");
		if(loginUser==null) {
			ObjectMapper objectMapper=new ObjectMapper();
			String json=objectMapper.writeValueAsString(ResultBean.ERROR(50001,"未登录"));

			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(json);
			return false;
		}
		return true;
	}
}
